<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        ul,li {list-style: none;}
        * {
            margin: 0;
            padding: 0;
        }
        #wrap {
            position: relative;
            width: 300px;
            height: 300px;
            border: 1px solid pink;
            margin: 50px auto;
        }
        #wrap .con {
            position: absolute;
            top: 30px;
            left: 0;
            width: 300px;
            height: 270px;
        }
        #wrap .con li {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 300px;
            height: 270px;
        }
        #wrap .con li.active {
            display: block;
        }
        #wrap .tab {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 30px;
        }
        #wrap .tab li {
            float: left;
            width: 99px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            font-size: 14px;
            border-right: 1px solid #fff;
            background-color: pink;
            color: #fff;
            cursor: pointer;
        }
        #wrap .tab li.active {
            background-color: #f60;
        }
    </style>
</head>
<body>
    <div id="wrap">
        <div class="con">
            <ul>
                <li class="active" style="background-color: red">A内容</li>
                <li style="background-color: green">B内容</li>
                <li style="background-color: blue">C内容</li>
            </ul>
        </div>

        <div class="tab">
            <ul>
                <li class="active">A</li>
                <li>B</li>
                <li>C</li>
            </ul>
        </div>
    </div>

    <script>
    
        (function() {

            //基础的Tab类
            function Tab(param) {
                this.conEle = param.conEle;
                this.tabEle = param.tabEle;
                this.conClass = param.conClass || "active";
                this.tabClass = param.tabClass || "active";
                this.length = this.tabEle.length;
                this.index = 0;
                this.exe();
            }
            Tab.prototype = {
                constructor: Tab,

                //构造函数方便启动
                exe: function() {
                    this.addClick();
                },
                //tab添加点击事件
                addClick: function() {
                    for(var i=0;i<this.length;i++) {
                        (function(i) {
                            this.tabEle[i].onclick = function() {
                                this.change(i);
                            }.bind(this);
                        }).call(this,i);
                    }
                },
                //变化函数
                change: function(i) {
                    if(this.index === i) {
                        return;
                    } 
                    
                    this.conEle[this.index].classList.remove(this.conClass);
                    this.tabEle[this.index].classList.remove(this.tabClass);
                    this.index = i;
                    this.conEle[this.index].classList.add(this.conClass);
                    this.tabEle[this.index].classList.add(this.tabClass);
                }
            };

            //实例测试
            new Tab({
                conEle: document.querySelectorAll('#wrap .con li'),
                tabEle: document.querySelectorAll('#wrap .tab li')
            });
        })();

    </script>
</body>
</html>